package net.main.dartcounter;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.JOptionPane;

public class GameHandler {
	private String name1 = null;
	private String name2 = null;
	private int score1;
	private int score2;
	private int thrownArrows1;
	private int thrownArrows2;
	
	private int startScore;
	private int turn;
	
	private DartCounter instance;
	
	public GameHandler(DartCounter instance) {
		this.instance = instance;
		startGame(501, true);
	}
	
	public GameHandler(DartCounter instance, int startScore) {
		this.instance = instance;
		startGame(startScore, true);
	}
	
	public void restartGame() {
		String stringStartScore = JOptionPane.showInputDialog("What score do you want to start with?");
		
		boolean newNames;
		
		for(;;) {
			if(instance.is_Numeric(stringStartScore)) {
				startScore = instance.convert_to_Numeric(stringStartScore);
				break;
			} else JOptionPane.showMessageDialog(instance, "Please enter a number!");
		}
		
		switch(JOptionPane.showConfirmDialog(instance, "Are you playing with the same 2 players?", "Same players?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) {
		case 0:
			newNames = false;
			break;
		default:
			newNames = true;
		}
		startGame(startScore, newNames);
		instance.updateUI();
	}
	
	public void startGame(int startScore, boolean newNames) {
		score1 = 0;
		score2 = 0;
		thrownArrows1 = 0;
		thrownArrows2 = 0;
		
		setTurn(1);
		
		String stringStartScore;
		for(;;) {
			if(startScore > 100) {
				break;
			} else JOptionPane.showMessageDialog(instance, "You have to start with at least a score of 101.");
			
			stringStartScore = JOptionPane.showInputDialog("What score would you like to start with?");
			if(!instance.is_Numeric(stringStartScore)) {
				startScore = -1;
			} else {
				startScore = instance.convert_to_Numeric(stringStartScore);
			}
		}
		
		if(newNames) {
			newNames();
		}
		
		this.startScore = startScore;
		
		setScore(startScore, 1);
		setScore(startScore, 2);
	}
	
	public void newNames() {
		for(;;) {
			name1 = JOptionPane.showInputDialog("Player 1 would be..");
			
			if(name1.equalsIgnoreCase("")) {
				JOptionPane.showMessageDialog(instance, "Invalid name!");
			} else {
				break;
			}
		}
		
		for(;;) {
			name2 = JOptionPane.showInputDialog("Player 2 would be..");
			if(name2.equalsIgnoreCase("")) {
				JOptionPane.showMessageDialog(instance, "Invalid name!");
			} else {
				break;
			}
		}
	}
	
	public String getName(int player) {
		switch(player) {
			case 1:
				return name1;
			case 2:
				return name2;
			default:
				return "Noname - Error!";
		}
	}
	
	public int getScore(int player) {
		switch(player) {
			case 1:
				return score1;
			case 2:
				return score2;
			default:
				JOptionPane.showMessageDialog(instance, "An error has occured while getting the score!");
				return -1;
		}
	}
	
	public void setScore(int score, int player) {
		switch(player) {
			case 1:
				score1 = score;
				break;
			case 2:
				score2 = score;
				break;
			default:
				JOptionPane.showMessageDialog(instance, "An error has occured while setting the score! Player: " + player);
		}
	}
	
	public void contractScore(int player) {
		switch(player) {
			case 1:
				setScore(getScore(1) - instance.getThrown(player), 1);
				instance.thrownScore.setText("");
				break;
			case 2:
				setScore(getScore(2) - instance.getThrown(player), 2);
				instance.thrownScore.setText("");
				break;
			default:
				JOptionPane.showMessageDialog(instance, "An error has occured!");
		}
	}
	
	public void addThrownArrows(int arrows, int player) {
		switch(player) {
		case 1:
			thrownArrows1 = thrownArrows1 + arrows;
			break;
		case 2:
			thrownArrows2 = thrownArrows2 + arrows;
			break;
		default:
			JOptionPane.showMessageDialog(instance, "An error has occured while adding arrows");
		}
	}
	
	public String getAveragePerArrow(int player) {
		
		double thrownScore;
		double average;
		
		switch(player) {
		case 1:
			thrownScore = (double) (startScore - score1);
			
			if(thrownArrows1 > 0) average = (double) (thrownScore / thrownArrows1);
			else average = 0.00;
			break;
		case 2:
			thrownScore = startScore - score2;
			if(thrownArrows2 > 0) average = thrownScore / thrownArrows2;
			else average = 0.00;
			break;
		default:
			JOptionPane.showMessageDialog(instance, "An error has occured while getting avarage per arrow!");
			return "-1";
		}
		
		NumberFormat formatter = new DecimalFormat("##0.00");
		return formatter.format(average);
	}
	
	public int getThrownArrows(int player) {
		switch(player) {
		case 1:
			return thrownArrows1;
		case 2:
			return thrownArrows2;
		default:
			JOptionPane.showMessageDialog(instance, "An error occured while getting amount of thrown arrows!");
			return -1;
		}
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}
	
	public void nextTurn() {
		switch(getTurn()) {
		case 1:
			setTurn(2);
			break;
		case 2:
			setTurn(1);
			break;
		default:
			JOptionPane.showMessageDialog(instance, "An error has occured while setting the turn!");
		}
	}
}
